<?php header("Content-Type:text/html;   charset=utf-8"); ?>
<script type="text/javascript">
	$(function(){		
		$("#txtUserNameNewAdmin").bind("blur",checkUserNameNewAdmin);
		$("#txtPwdNewAdmin").bind("blur",checkPwdNewAdmin);		
		loadAdminNewAdmin();
		$("#btnSubmitNewAdmin").bind("click",submitAdmin);
	});	
	function submitAdmin(){		
		if(checkUserNameNewAdmin()==false)
			return;
		if(checkPwdNewAdmin()==false)
			return;
		var id = $("#hdidadmin").val();
		var username = $("#txtUserNameNewAdmin").val();		
		var pwd = $("#txtPwdNewAdmin").val();		
		var params = {
					id:id,
					username:username,
					pwd:pwd,
					isvalid:$("input[type='radio'][name^='status']:checked").val()
				};			
		
		$.ajax({
			type:"post",
			url:"admin.php/manage/addAdmin",
			dataType:"json",
			data:params,
			success:function(data){
				parent.loadAdmin();
				alert("提交成功");
				$("#btnCloseNewAdmin").trigger("click");
			}
		});
	}
	
	function loadAdminNewAdmin(){
		var id = $("#hdidadmin").val();
		if(id==0)
			return;
		$("#txtUserNameNewAdmin").attr("disabled","disabled");
		var params = {id:id};
		$.ajax({
			type:"post",
			url:"admin.php/manage/getAdminById",
			dataType:"json",
			data:params,
			success:function(data){
				$("#txtUserNameNewAdmin").val(data.username);
				$("#txtPwdNewAdmin").val(data.pwd);
				$("#status"+data.isvalid).trigger("click");
			}
		});
	}
	function checkUserNameNewAdmin(){
		var username = $("#txtUserNameNewAdmin").val();
		if(IsEmpty(username)){
			SetError("txtUserNameNewAdmin","账号不能为空");
			return false;
		}
		var len = username.length;
		if(len>20){
			SetError("txtUserNameNewAdmin","最多20字符");
			return false;
		}
		var id = $("#hdidadmin").val();
		var params = {
					id:id,
					username:username
				};
		var isok=true;
		$.ajax({
			type:"post",
			url:"admin.php/manage/checkExistsAdmin",
			dataType:"json",
			data:params,
			async:false,
			success:function(data){				
				if(data.ReturnCode!="1"){
					isok=false;
					SetError("txtUserNameNewAdmin","该用户名已存在");
				}
			}
		});
		if(isok==false)
			return false;
		
		SetOK("txtUserNameNewAdmin");
		return true;
	}
	function checkPwdNewAdmin(){
		var pwd = $("#txtPwdNewAdmin").val();
		if(IsEmpty(pwd)){
			SetError("txtPwdNewAdmin","密码不能为空");
			return false;
		}		
		var len = pwd.length;
		if(len>20){
			SetError("txtPwdNewAdmin","密码最长20个字符");
		}
		SetOK("txtPwdNewAdmin");
		return true;
	}
	
</script>
<div class="pageContent">
	<form class="pageForm required-validate">
		<div class="pageFormContent" style="width: 98%;" layoutH="56">
			<table class="tableNewUser">
				<tr>
					<td>管理员账号：</td>
					<td><input id="txtUserNameNewAdmin" type="text" value="" /></td>
				</tr>
				<tr>
					<td>管理员密码：</td>
					<td><input id="txtPwdNewAdmin" type="text" value="" /></td>
				</tr>
				<tr>
					<td>状态：</td>
					<td><input type="radio" checked name='status' id="status1" value="1" style="width:20px"><span for="status1">有效</span>
					<input type="radio" name='status' id="status0" value="0" style="width:20px"><span for="status0">无效</span></td>
				</tr>

				<tr>
					<td colspan="2" style='line-height: 40px'>&nbsp;&nbsp;</td>
				</tr>
			</table>
			<input type="hidden" id="hdidadmin"
				value="<?php if(isset($_GET["id"])) print $_GET["id"]; else print 0;?>">

			<div class="buttonActive" style="width: 120px; margin-left: 50px;">
				<div class="buttonContent">
					<button id="btnSubmitNewAdmin" type="button" style="width: 80px">提
						交</button>
				</div>
			</div>

			<div class="button" style="text-align: right">
				<div class="buttonContent" style="text-align: right">
					<button id="btnCloseNewAdmin" type="button" class="close"
						style="width: 80px">取 消</button>
				</div>
			</div>
		</div>
	</form>
</div>
